package cn.ylz.ylmall.order.controller;

import cn.ylz.ylmall.account.security.CurrentPrincipal;
import cn.ylz.ylmall.common.web.JsonResult;
import cn.ylz.ylmall.order.pojo.entity.OrderDetail;
import cn.ylz.ylmall.order.pojo.request.OrderCreateReq;
import cn.ylz.ylmall.order.pojo.request.SingleOrderReq;
import cn.ylz.ylmall.order.pojo.vo.OrderDetailVO;
import cn.ylz.ylmall.order.service.OrderDetailService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 订单控制类
 * @author ks
 */
@RestController
@Slf4j
@RequestMapping("/v1/Order")
@Validated
@Api(tags = "5.1. 订单模块")
public class OrderController {

    @Autowired
    private OrderDetailService orderDetailService;


    @PostMapping("/create")
    @ApiOperation("新增订单")
    @ApiOperationSupport(order = 501)
    public JsonResult insertOrder(@AuthenticationPrincipal CurrentPrincipal currentPrincipal,
            @Valid @RequestBody OrderCreateReq orderCreateReq){
        log.debug("开始处理【新增订单】的请求，参数：{}", orderCreateReq);
        Long userId = currentPrincipal.getId();
        //返回订单号
        return JsonResult.ok(orderDetailService.insertOrder(orderCreateReq,userId));
    }

    @GetMapping("/select/detail")
    @ApiOperation("查询订单")
    @ApiOperationSupport(order = 502)
    public JsonResult selectOrder(@AuthenticationPrincipal CurrentPrincipal currentPrincipal,
            @RequestParam("orderNo") String orderNo){
        log.debug("开始处理【查询订单】的请求，参数：{}", orderNo);
        Long userId = currentPrincipal.getId();
        OrderDetailVO orderDetailVO = orderDetailService.selectOrder(orderNo,userId);
        return JsonResult.ok(orderDetailVO);
    }

    @GetMapping("/select/List")
    @ApiOperation("查询订单列表")
    @ApiOperationSupport(order = 503)
    public JsonResult selectOrderList(@AuthenticationPrincipal CurrentPrincipal currentPrincipal,
                      @RequestParam() Integer pageNum,@RequestParam Integer pageSize){
        log.debug("开始处理【查询订单列表】的请求,参数:{},{}",pageNum,pageSize);
        Long userId = currentPrincipal.getId();
        return JsonResult.ok(orderDetailService.selectOrderList(userId,pageNum,pageSize));
    }


    @PostMapping("/delete")
    @ApiOperation("删除订单")
    @ApiOperationSupport(order = 504)
    public JsonResult deleteOrder(@AuthenticationPrincipal CurrentPrincipal currentPrincipal,
                                  @RequestParam("orderNo") String orderNo){
        log.debug("开始处理【删除订单】的请求，参数：{}", orderNo);
        Long userId = currentPrincipal.getId();
        int num = orderDetailService.deleteOrder(orderNo,userId);
        return JsonResult.ok(num);
    }

    @PostMapping("/update")
    @ApiOperation("修改订单")
    @ApiOperationSupport(order = 505)
    public JsonResult updateOrder(@AuthenticationPrincipal CurrentPrincipal currentPrincipal,
                                  @RequestBody OrderDetail orderDetail){
        log.debug("开始处理【修改订单】的请求，参数：{}", orderDetail);
        Long userId = currentPrincipal.getId();
        int num = orderDetailService.updateOrder(orderDetail,userId);
        return JsonResult.ok(num);
    }

    @PostMapping("/qrcode")
    @ApiOperation("生成支付二维码")
    @ApiOperationSupport(order = 506)
    public JsonResult getQrcode(@AuthenticationPrincipal CurrentPrincipal currentPrincipal,
                                @RequestParam("orderNo") String orderNo){
        log.debug("开始处理【生成支付二维码】的请求，参数：{}", orderNo);

        return JsonResult.ok(orderDetailService.getQrCode(orderNo));
    }

    @GetMapping("/select/")
    @ApiOperation("查询订单状态")
    @ApiOperationSupport(order = 507)
    public JsonResult selectOrderStatus(Integer orderStatus,@RequestParam() Integer pageNum,@RequestParam Integer pageSize){
        log.debug("开始处理【查询订单列表】的请求,参数:{}",orderStatus);
        return JsonResult.ok(orderDetailService.selectOrderStatus(orderStatus, pageNum,pageSize));
    }

    @PostMapping("/cancel")
    @ApiOperation("取消订单")
    @ApiOperationSupport(order = 508)
    public JsonResult cancelOrder(@AuthenticationPrincipal CurrentPrincipal currentPrincipal,
                                  @RequestParam("orderNo") String orderNo){
        log.debug("开始处理【取消订单列表】的请求,参数:{}",orderNo);
        Long userId = currentPrincipal.getId();
        int num = orderDetailService.cancelOrder(userId,orderNo);
        return JsonResult.ok(num);
    }

    @PostMapping("/pay")
    @ApiOperation("支付订单")
    @ApiOperationSupport(order = 509)
    public JsonResult payOrder(@RequestParam("orderNo") String orderNo){
        log.debug("开始处理【支付订单列表】的请求,参数:{}",orderNo);
        int num = orderDetailService.payOrder(orderNo);
        return JsonResult.ok(num);
    }

    @PostMapping("/created/single")
    @ApiOperation("创建单个商品订单")
    @ApiOperationSupport(order = 510)
    public JsonResult createdSingleOrder(@AuthenticationPrincipal CurrentPrincipal currentPrincipal,
            @RequestBody SingleOrderReq singleOrderReq){
        Long userId = currentPrincipal.getId();
        log.debug("开始处理【支付订单列表】的请求,参数:{}",singleOrderReq);
        String orderNo = orderDetailService.createdSingleOrder(singleOrderReq,userId);
        return JsonResult.ok(orderNo);
    }


}
